package com.lc.ck.controller;



import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lc.ck.common.QueryPageParam;
import com.lc.ck.common.Result;
import com.lc.ck.entity.Goodstype;
import com.lc.ck.entity.User;
import com.lc.ck.service.GoodstypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * (Goodstype)表控制层
 *
 * @author makejava
 * @since 2023-03-07 15:06:13
 */

@RestController
@RequestMapping("/goodstype")
public class GoodstypeController{
    @Autowired
    private GoodstypeService goodstypeService;

    @PostMapping("/save")
    public Result save(@RequestBody Goodstype goodstype){

        return goodstypeService.save(goodstype)?Result.suc():Result.fail();
    }
    //修改
    @PostMapping("/mod")
    public boolean mod(@RequestBody Goodstype goodstype){
        return goodstypeService.updateById(goodstype);
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Goodstype goodstype){

        return goodstypeService.updateById(goodstype)?Result.suc():Result.fail();
    }
    //新增或修改
    @PostMapping("/saveOrMod")
    public  boolean saveOrMod(@RequestBody Goodstype goodstype){
        return goodstypeService.saveOrUpdate(goodstype);
    }
    //删除
    @GetMapping("/delete")
    public Result delete(@RequestParam  String id){
        return goodstypeService.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/listPlist")//返回数据封装
    public Result listPlist(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");

        Page<Goodstype> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<Goodstype> lambdaQueryWrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){//非空判断和非 null 判断
            lambdaQueryWrapper.like(Goodstype::getName,name);
        }

        IPage result = goodstypeService.pageCC(page,lambdaQueryWrapper);

        return Result.suc(result.getRecords(),result.getTotal());//成功时将数据和总数返回
    }
    @GetMapping("/list")
    public Result list(){
        List list =  goodstypeService.list();
        return Result.suc(list);//当有数据时传回list数据，前端将接受200
    }
}

